<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/10/10
 * Time: 16:36
 */
ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

// Set time limit to indefinite execution
set_time_limit(0);

// Set the ip and port we will listen on
$address = 'localhost';
$port = 6901;

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
// Non block socket type
socket_set_nonblock($sock);
// Loop continuously
while (true) {
    if ($newsock = @socket_accept($sock)) {
        if (is_resource($newsock)) {
            socket_write($newsock, "$j>", 2) . chr(0);
            echo "New client connected $j";
            $client[$j] = $newsock;
            $j++;
        }
    }
    if (count($client)) {
        foreach ($client AS $k => $v) {
            if (@socket_recv($v, $string, 1024, MSG_DONTWAIT) === 0) {
                unset($client[$k]);
                socket_close($v);
            } else {
                if ($string) {
                    echo "$k: $string\n";
                }
            }
        }
    }
    sleep(1);
    socket_write($newsock,(string)date("Y-m-d H:i:s"),1024);
    echo "".date("Y-m-d H:i:s")." waitting...\n...";
}
socket_close($sock);
